<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable width="70%">
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="110px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="8" class="mb20">
					<el-form-item label="设备名称" prop="equipmentName">
						<el-input v-model="form.equipmentName" placeholder="请输入设备名称" />
					</el-form-item>
				</el-col>

				<el-col :span="8" class="mb20">
					<el-form-item label="是否自动派单" prop="autoDispatch">
						<el-select v-model="form.autoDispatch" placeholder="请选择是否自动派单">
							<el-option :value="0" label="自动" />
							<el-option :value="1" label="手动" />
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="8" class="mb20">
					<el-form-item label="派单间隔(分)" prop="dispatchInterval">
						<el-input-number :min="1" :max="1000" v-model="form.dispatchInterval" placeholder="请输入派单间隔"></el-input-number>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<el-table
			v-if="!!form.id"
			:data="form.airOverThresholdList"
			max-height="600"
			v-loading="loading"
			border
			:cell-style="{ textAlign: 'center' }"
			:header-cell-style="{
				textAlign: 'center',
				background: 'var(--el-table-row-hover-bg-color)',
				color: 'var(--el-text-color-primary)',
			}"
		>
			<el-table-column type="index" label="序号" width="60" />
			<el-table-column prop="indicatorCnName" label="指标中文名" width="150">
				<template #default="scope">
					<el-input v-model="scope.row.indicatorCnName" placeholder="请输入" size="small" />
				</template>
			</el-table-column>
			<el-table-column prop="indicatorEnName" label="指标英文名" width="150">
				<template #default="scope">
					<el-input v-model="scope.row.indicatorEnName" placeholder="请输入" size="small" />
				</template>
			</el-table-column>
			<el-table-column prop="unit" label="单位" width="150">
				<template #default="scope">
					<el-input v-model="scope.row.unit" placeholder="请输入" size="small" />
				</template>
			</el-table-column>
			<el-table-column prop="point" label="精度" width="150">
				<template #default="scope">
					<el-input-number :min="0" :max="6" v-model="scope.row.point" placeholder="请输入" size="small" />
				</template>
			</el-table-column>
			<el-table-column prop="alarmLimit" label="警报值" width="150">
				<template #default="scope">
					<el-input-number :min="0" :max="9999" v-model="scope.row.alarmLimit" placeholder="请输入" size="small" />
				</template>
			</el-table-column>
			<el-table-column prop="measuringLower" label="下限" width="150">
				<template #default="scope">
					<el-input-number :min="0" :max="9999" v-model="scope.row.measuringLower" placeholder="请输入" size="small" />
				</template>
			</el-table-column>
			<el-table-column prop="measuringUpper" label="上限" width="150">
				<template #default="scope">
					<el-input-number :min="0" :max="9999" v-model="scope.row.measuringUpper" placeholder="请输入" size="small" />
				</template>
			</el-table-column>
		</el-table>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="AirAlarmSettingDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj } from '/@/api/platform/airAlarmSetting';
import { rule } from '/@/utils/validate';

const emit = defineEmits(['refresh']);
// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
// 定义字典

// 提交表单数据
const form = reactive({
	id: '',
	equipmentName: '',
	equipmentType: 0,
	autoDispatch: 0,
	dispatchInterval: 0,
	isOverLimitSetting: 1,
});

// 定义校验规则
const dataRules = ref({
	equipmentName: [{ required: true, message: '请输入', trigger: 'blur' }],
	dispatchInterval: [{ required: true, message: '请输入', trigger: 'blur' }],
});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.id = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取airAlarmSetting信息
	if (id) {
		form.id = id;
		getairAlarmSettingData(id);
	}
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getairAlarmSettingData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
